package com.ubi.app;

import com.ubi.core.framework.DefaultSiteWebConfig;
import com.ubi.core.framework.request.RequestContext;
import com.ubi.core.framework.request.RequestContextInterceptor;
import org.apache.commons.lang3.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

/**
 * 配置文件
 *
 * @author Kent
 */
@Configuration
public class WebConfig extends DefaultSiteWebConfig {

    @Autowired
    Environment environment;
    //
    // @Bean
    // public SiteSettings siteSettings() {
    // SiteSettings siteSettings = new SiteSettings();
    // siteSettings.setUploadFilePath(environment.getRequiredProperty("filePath"));
    // siteSettings.setDocumentCoursewareFileType(environment.getRequiredProperty("documentCoursewareFileType"));
    // siteSettings.setVideoCoursewareFileType(environment.getRequiredProperty("videoCoursewareFileType"));
    // return siteSettings;
    // }
    //
    // @Bean
    // public UploadFileHandler uploadFileHandler() {
    // return new UploadFileHandler();
    // }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxInMemorySize(4096);
        // multipartResolver.setMaxUploadSize(1048576 * 2); // 最大尺寸为2M
        return multipartResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/libs/**").addResourceLocations("/libs/");
        registry.addResourceHandler("/module/**").addResourceLocations("/module/");
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        // registry.addResourceHandler(siteSettings().getUploadFilePath() + "/**")
        // .addResourceLocations(siteSettings().getUploadFilePath() + "/");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    public DefaultServletHttpRequestHandler defaultServletHttpRequestHandler() {
        DefaultServletHttpRequestHandler delDefaultServletHttpRequestHandler = new DefaultServletHttpRequestHandler();
        return delDefaultServletHttpRequestHandler;
    }

//    @Override
//    public void addCustomInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(loginRequireInterceptor());
//    }
//
//    @Bean
//    public LoginRequireInterceptor loginRequireInterceptor() {
//        LoginRequireInterceptor loginRequireInterceptor=new LoginRequireInterceptor();
//        return loginRequireInterceptor;
//    }

    @Bean
    @Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public RequestContext requestContext() {
        return new RequestContext();
    }

    @Bean
    public RequestContextInterceptor requestContextInterceptor() {
        return new RequestContextInterceptor();
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages/common_messages", "classpath:messages/messages");
        messageSource.setDefaultEncoding(CharEncoding.UTF_8);
//        messageSource.setFallbackToSystemLocale(false);
        return messageSource;
    }

}
